package com.yangyang.androidconnect.data;

import com.yangyang.androidconnect.vo.response.LoginUserResponse;

import org.apache.commons.lang3.StringUtils;

import java.util.Arrays;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.UUID;

public class UserManager {
    private static final Map<String, String> userMap;
    private static final Map<String, List<String>> roleMap;

    static {
        userMap = new HashMap<>();
        roleMap = new HashMap<>();
        add("1111", "1111", "admin");
        add("guest", "guest", "guest");
    }

    private static void add(String username, String password, String... roleList) {
        userMap.put(username, password);
        if (roleList.length > 0) {
            List<String> list = roleMap.getOrDefault(username, new LinkedList<>());
            list.addAll(Arrays.asList(roleList));
            roleMap.put(username, list);
        }
    }

    public static LoginUserResponse getLoginUser(String username, String password) throws Exception {
        LoginUserResponse response = new LoginUserResponse();
        String s = userMap.get(username);
        if (s == null) throw new Exception("用户不存在");
        if (!StringUtils.equals(password, s)) throw new Exception("用户不存在");

        response.setUsername(username);
        response.setToken(UUID.randomUUID().toString());
        response.setRoles(roleMap.getOrDefault(username, new LinkedList<>()));
        return response;
    }

}
